﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MDIDemo.Modules
{
    public partial class PhanQuyen : DevExpress.XtraEditors.XtraForm
    {
        string sErr = "";

        int idThem = 4;
        int idSua = 9;
        int idXoa = 6;
        void SetPermission()
        {
            if (this.Parent.Parent is frmMain)
            {
                frmMain main = (frmMain)this.Parent.Parent;
                btnSua.Visible = frmMain.IsPermis(idSua, main.CurrentPermis.PERMISSION);
                btnLuu.Visible = frmMain.IsPermis(idSua, main.CurrentPermis.PERMISSION);
            }
        }
        public PhanQuyen()
        {
            InitializeComponent();

        }

        private void gvGroup_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle > -1)
            {
                DTO.POGInfo inf = new DTO.POGInfo(gvGroup.GetDataRow(e.FocusedRowHandle));
                SetDataGroup(inf.ROLE_ID);
            }
        }

        private void SetDataGroup(string role)
        {
            BUS.POPControl ctr = new BUS.POPControl();
            DTO.POPInfo inf = ctr.Get(role, "", ref sErr);
            if (inf.ROLE_ID != "")
            {
                DataTable dt = gridPermis.DataSource as DataTable;
                foreach (DataRow row in dt.Rows)
                {
                    int index = Convert.ToInt32(row["ID"]);
                    if (inf.PERMISSION[index] == 'Y')
                        row["Permis"] = true;
                    else row["Permis"] = false;

                }
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            EnabledForm(true);
        }

        private void EnabledForm(bool p)
        {
            gridGroup.Enabled = !p;
            btnSua.Enabled = !p;
            gvPermis.OptionsBehavior.ReadOnly = !p;
            btnThemChiTiet.Enabled = p;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            sErr = "";
            DTO.POPInfo inf = GetData();
            BUS.POPControl ctr = new BUS.POPControl();
            sErr = ctr.InsertUpdate(inf);
            if (sErr == "")
                XtraMessageBox.Show("Lưu thành công", "Thông báo");
            else XtraMessageBox.Show(sErr, "Lỗi");
            EnabledForm(false);
        }

        private DTO.POPInfo GetData()
        {
            DTO.POPInfo inf = new DTO.POPInfo();
            int[] currentRows = gvGroup.GetSelectedRows();
            if (currentRows != null && currentRows.Length > 0)
            {
                DTO.POGInfo infG = new DTO.POGInfo(gvGroup.GetDataRow(currentRows[0]));
                inf.ROLE_ID = infG.ROLE_ID;
                inf.DB = "";

                if (inf.PERMISSION == "")
                    inf.PERMISSION = new string(' ', 500);
                inf.PERMISSION = GetPermission(inf.PERMISSION);
            }
            return inf;
        }

        private string GetPermission(string result)
        {
            //result = "Y";
            DataTable dt = gridPermis.DataSource as DataTable;
            foreach (DataRow row in dt.Rows)
            {
                int index = Convert.ToInt32(row["ID"]);
                if (row["Permis"] != DBNull.Value && (bool)row["Permis"] == true)
                    result = result.Insert(index, "Y");
                else result = result.Insert(index, "N");
                result.Remove(result.Length - 1, 1);

            }
            return result;
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            EnabledForm(false);
        }

        private void PhanQuyen_Load(object sender, EventArgs e)
        {
            LoadPermisDGV();
            EnabledForm(false);
            BUS.POGControl ctr = new BUS.POGControl();
            gridGroup.DataSource = ctr.GetAll(ref sErr);
            SetPermission();
        }

        private void LoadPermisDGV()
        {
            string pathXML = frmMain.ConfigPath + "\\Security.xml";
            DataTable dt = new DataTable("POP");
            dt.Columns.AddRange(new DataColumn[] { new DataColumn("ID"), new DataColumn("Module"), new DataColumn("Function"), new DataColumn("Action"), new DataColumn("Permis", typeof(bool)) });
            dt.ReadXml(pathXML);
            gridPermis.DataSource = dt;
        }

        private void btnThemChiTiet_Click(object sender, EventArgs e)
        {
            DataTable dt = gridPermis.DataSource as DataTable;
            if (dt != null)
            {
                foreach (DataRow row in dt.Rows)
                {
                    row["Permis"] = true;
                }
            }

        }

        private void PhanQuyen_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if (btnBoQua.Enabled)
                    btnBoQua_Click(null, null);

            }
            else if (e.KeyCode == Keys.F7)
            {
                if (btnLuu.Enabled)
                    btnLuu_Click(null, null);

            }
        }
    }
}